﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace 数据库操作
{
    [Serializable]
    public class 温度数据Data
    {

        public const string CALLERField = "@Caller";

        public const string CALLINTIMEField = "@CallInTime";

        public const string TEMPERATUREField = "@Temperature";

        public const string ERRFLAGField = "@ErrFlag";

        private String _Caller;

        private DateTime _CallInTime;

        private Single _Temperature;

        private Byte _ErrFlag;

        public String Caller
        {
            get { return _Caller; }
            set { _Caller = value; }
        }
        public DateTime CallInTime
        {
            get { return _CallInTime; }
            set { _CallInTime = value; }
        }
        public Single Temperature
        {
            get { return _Temperature; }
            set { _Temperature = value; }
        }
        public Byte ErrFlag
        {
            get { return _ErrFlag; }
            set { _ErrFlag = value; }
        }
        public 温度数据Data()
        {
            _Caller = "";
            _CallInTime = DateTime.Now;
            _Temperature = 0;
            _ErrFlag = 0;
        }
    }

    public class 温度数据DataFactory
    {
        public static 温度数据Data Construct(IDataReader reader)
        {
            温度数据Data data = new 温度数据Data();

            try { data.Caller = Convert.ToString(reader["Caller"]); }
            catch { }

            try { data.CallInTime = Convert.ToDateTime(reader["CallInTime"]); }
            catch { }

            try { data.Temperature = Convert.ToSingle(reader["Temperature"]); }
            catch { }

            try { data.ErrFlag = Convert.ToByte(reader["ErrFlag"]); }
            catch { }

            return data;
        }
        public static 温度数据Data Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static 温度数据Data Construct(DataTable dt, int rowIndex)
        {
            温度数据Data data = new 温度数据Data();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.Caller = Convert.ToString(dr["Caller"]); }
            catch { }

            try { data.CallInTime = Convert.ToDateTime(dr["CallInTime"]); }
            catch { }

            try { data.Temperature = Convert.ToSingle(dr["Temperature"]); }
            catch { }

            try { data.ErrFlag = Convert.ToByte(dr["ErrFlag"]); }
            catch { }

            return data;
        }
        public static 温度数据Data Construct(DataRow dr)
        {
            温度数据Data data = new 温度数据Data();

            try { data.Caller = Convert.ToString(dr["Caller"]); }
            catch { }

            try { data.CallInTime = Convert.ToDateTime(dr["CallInTime"]); }
            catch { }

            try { data.Temperature = Convert.ToSingle(dr["Temperature"]); }
            catch { }

            try { data.ErrFlag = Convert.ToByte(dr["ErrFlag"]); }
            catch { }

            return data;
        }
    }
    public class 温度数据DataDBOption
    {
        public static int Insert(温度数据Data d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into 温度数据 (");
            sql.Append("Caller, ");
            sql.Append("CallInTime, ");
            sql.Append("Temperature, ");
            sql.Append("ErrFlag");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.Caller.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.CallInTime.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.Temperature.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.ErrFlag.ToString());
            sql.Append("");
            sql.Append(")");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int InsertCall(温度数据Data d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into 呼叫列表 (");
            sql.Append("Caller, ");
            sql.Append("CallInTime, ");
            sql.Append("Temperature, ");
            sql.Append("ErrFlag");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.Caller.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.CallInTime.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.Temperature.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.ErrFlag.ToString());
            sql.Append("");
            sql.Append(")");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 温度数据 ");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataTable DataTableSelect_GetLastDataTime()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select top 1 [CallInTime] from 温度数据 order by CallInTime DESC");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataTable DataTableSelect_GetLastData(string Caller)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select top 1 [Temperature],[CallInTime] from 温度数据 where Caller='" + Caller + "' order by CallInTime DESC");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 温度数据 ");
            return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
        }
        public static SqlDataReader ReaderSelect(温度数据Data d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 温度数据 ");
            return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
        }
    }
}